package com.xiaotu.storage;

import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import com.xiaotu.storage.model.CacheDataModel;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @类名 MOVPublicStorageProcessor
 * @日期 2017年2月27日
 * @作者 高海军
 * @功能 电影备案发行数据转储实现类
 */
@Component("MOVPublicStorageProcessor")
public class MOVPublicStorageProcessor extends AHSetStorageProcessor
{
    private static final String[] FIELD_ARR = new String[]{"tvName", "type", "licenseNum", "screenwriter",
            "production", "pubMonth", "result", "area", "introduction", "timestamp"};

    private static final String[] CONVERT_ARR = new String[]{AConverter.STRING_CONVERT, AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter
            .STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.STRING_CONVERT,
            AConverter.TIMESTAMP_CONVERT};

    private static final boolean[] THROW_ARR = new boolean[]{true, true, true, true, true, true, true, true, true,
            true};

    @Override
    protected String getRedisKey(Date date)
    {
        return DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOV_Public_YYYYMMDD, date);
    }

    private int getMovType(String typeName)
    {
        int mvType = 0;
        if (typeName.contains("故事"))
            mvType = 1;
        else if (typeName.contains("动画"))
            mvType = 2;
        else if (typeName.contains("纪录"))
            mvType = 3;
        else if (typeName.contains("科教"))
            mvType = 4;
        else if (typeName.contains("特种"))
            mvType = 5;
        else if (typeName.contains("合拍"))
            mvType = 6;
        return mvType;
    }

    @Override
    public String process(JSONObject target, CacheDataModel cacheData, Date date)
    {
        this.validateAllNotEmpty(target, FIELD_ARR);
        target.put(FIELD_ARR[1], this.getMovType(target.getString(FIELD_ARR[1])));
        return this.list2CSVLine(ConverterFactory.convert(target, FIELD_ARR, CONVERT_ARR, THROW_ARR));
    }

    @Override
    public String getDataType()
    {
        return Constants.PageDataType.TV_RECORD;
    }

    @Override
    public String getTaskType()
    {
        return Constants.SpiderTaskType.MOV_PUBLIC_DATA;
    }
}
